package com.radiant.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.UriBuilder;

import com.google.inject.Inject;
import com.radiant.data.entity.Activity;
import com.radiant.data.entity.Activity.Type;
import com.radiant.data.entity.ActivityDAO;
import com.radiant.data.entity.ActivityFeedable;
import com.radiant.data.entity.Child;
import com.radiant.data.entity.ChildDAO;
import com.radiant.data.entity.Comment;
import com.radiant.data.entity.Like;
import com.radiant.data.entity.Measurement;
import com.radiant.data.entity.Milestone;
import com.radiant.data.entity.Post;
import com.radiant.data.entity.User;

@Path("/activity")
public class ActivityFeedServiceImpl implements ActivityFeedService {

	@Inject
	UserService userService;

	@Inject
	ChildDAO childDAO;

	@Inject
	ActivityDAO activityDAO;

	public List<String> getProtectedPaths() {
		List<String> paths = new ArrayList<String>();
		UriBuilder builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path("*").build().toString());
		return paths;
	}

	public void generateActivityItems(User user, Post post) {
		generateActivityItems(user, null, post, Type.POST);		
	}

	public void generateActivityItems(User user, Comment comment) {
		generateActivityItems(user, null, comment, Type.COMMENT);		
	}

	public void generateActivityItems(User user, Like like) {
		generateActivityItems(user, null, like, Type.LIKE);		
	}

	public void generateActivityItems(User user, Milestone milestone) {
		Child child = childDAO.findByPrimaryKey(milestone.getChildId(), true);
		generateActivityItems(user, child, milestone, Type.MILESTONE);
	}

	public void generateActivityItems(User user, Measurement measurement) {
		generateActivityItems(user, null, measurement, Type.MEASUREMENT);		
	}

	private void generateActivityItems(User user, Child child, ActivityFeedable activityFeedable, Activity.Type type) {
		List<User> users = new ArrayList<User>();
		if (user.getFriends().isEmpty()) {
			users.addAll(user.getFriends());
		}
		users.add(user);
		for (User u: users) {
			Activity activity = new Activity();
			activityFeedable.addMessageToActivity(activity, user, child);
			activityFeedable.addToActivity(activity);
			activity.setUserName(u.getUserName());
			activity.setCreator(user.getUserName());
			activity.setType(type);
			activity.setTimeCreated(new Date());
			activityDAO.save(activity);
		}	
	}
		

	@GET @Path("/all/topN")
	@Produces("application/json")
	public List<Activity> getActivityFeed(@QueryParam("count") int count) {
		String from = userService.getCurrentUser().getUserName();
		return activityDAO.getActivityFeed(from, count);
	}

	@GET @Path("/personal/topN")
	@Produces("application/json")
	public List<Activity> getPersonalActivityFeed(@QueryParam("count") int count) {
		String from = userService.getCurrentUser().getUserName();
		return activityDAO.getPersonalActivityFeed(from, count);
	}

}
